# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def diameterOfBinaryTree(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.res = 0
        self.find(root)
        return self.res

    def find(self,root):
        if not root:
            return 0
        max_left = self.find(root.left)
        max_right = self.find(root.right)
        h = max_left+max_right

        self.res = max(self.res,h)

        return max(max_left,max_right)+1
    #自己 参与度高的方法
    # class Solution:
    #     def diameterOfBinaryTree(self, root):
    #         """
    #         :type root: TreeNode
    #         :rtype: int
    #         """
    #         if not root:
    #             return 0
    #         res = self.find(root.left) + self.find(root.right)
    #         return max(res, max(self.diameterOfBinaryTree(root.left), self.diameterOfBinaryTree(root.right)))
    #
    #     def find(self, root):
    #         if not root:
    #             return 0
    #         max_left = self.find(root.left) + 1
    #         max_right = self.find(root.right) + 1
    #         return max(max_left, max_right)
m1 = TreeNode(5)
m1.left = TreeNode(2)
m1.right = TreeNode(13)
# m1.right.left = TreeNode(2)
# m1.right.right = TreeNode(3)
# m1.right.right.left = TreeNode(3)
s = Solution()

t = s.diameterOfBinaryTree(m1)
print(t)